from rest_framework import serializers
from django.contrib.auth import get_user_model
from .models import UserStarMovie, UserFavMovie, UserMovieShortComments
from guardian.shortcuts import get_perms
from movie.serializers import MovieListSerializer

User = get_user_model()


class UserStarCreateMovieSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = UserStarMovie
        fields = ("user", 'movie', 'star')


class UserStarReadMovieSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = UserStarMovie
        fields = ("user", 'movie', 'star', 'add_time')


class UserStarMovieUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserStarMovie
        fields = ('star',)


class UserFavMovieCreateSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    class Meta:
        model = UserFavMovie
        fields = ("user", "movie", "id")


class UserFavMovieReadSerializer(serializers.HyperlinkedModelSerializer):
    movie = MovieListSerializer(many=False)

    class Meta:
        model = UserFavMovie
        fields = ('url', "movie", "id")








class UserMovieShortCommentsSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )

    add_time = serializers.DateTimeField(read_only=True)

    def create(self, validated_data):
        user = self.context['request']._user

        data = user.has_perm('user_movies_operations.can_not_add_shortcomments')
        if data:
            raise serializers.ValidationError("你没有权限发帖,请联系管理员")

        return super().create(validated_data)

    class Meta:
        model = UserMovieShortComments
        fields = ('comments', 'user', 'movie', 'add_time', 'reply_to')


from guardian.shortcuts import get_perms


class UserMovieShortUpdateCommentsSerializer(serializers.ModelSerializer):

    def update(self, instance, validated_data):
        # select * from guradian_user_object where user=user and pk=ins.pk and cont_id = ins.cond
        permissions = get_perms(self.context['request']._user, instance)
        if 'can_update_shortcomments' not in permissions:
            raise serializers.ValidationError('你没有权限去修改')
        return super().update(instance, validated_data)

    class Meta:
        model = UserMovieShortComments
        fields = ('comments',)


class UserMovieShortCommentsReadSerializer(serializers.ModelSerializer):
    #
    #by:
    #
    # )

    can_delete = serializers.SerializerMethodField(
        read_only=True
    )

    can_update = serializers.SerializerMethodField(
        read_only=True
    )

    def get_can_delete(self, obj):
        user = self.context['request']._user

        permissions = get_perms(user, obj)

        return 'can_delete_shortcomments' in permissions

    def get_can_update(self, obj):
        user = self.context['request']._user
        permissions = get_perms(user, obj)
        return 'can_update_shortcomments' in permissions

    class Meta:
        model = UserMovieShortComments
        fields = ('comments', 'user', 'add_time', 'can_delete', 'can_update')

""""

string = "用户{},你的{}验证码为{}"

"""